package com.lebang.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author Eliohn on 2019/9/12.
 * @version 1.0
 */
public class SortUtil {

    /**
     * 根据Map的Key做升序排序
     *
     * @return
     */
    public static Map<String, String> sortMapByKeyAsc(Map<String, String> param) {
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(param.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
        list.sort(Comparator.comparing(Map.Entry::getKey));
//        list.sort((o1, o2) -> (o1.getKey()).compareTo(o2.getKey()));
        LinkedHashMap<String, String> set = new LinkedHashMap<String, String>();
        for (Map.Entry<String, String> m : list) {
            set.put(m.getKey(), m.getValue());
        }
        return set;
    }

    /**
     * 根据Map的Key做降序排序
     *
     * @return
     */
    public static Map<String, String> sortMapByKeyDesc(Map<String, String> param) {

        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(param.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从大到小排序（字典序）
        list.sort((o1, o2) -> (o2.getKey()).compareTo(o1.getKey()));
        LinkedHashMap<String, String> set = new LinkedHashMap<String, String>();
        for (Map.Entry<String, String> m : list) {
            set.put(m.getKey(), m.getValue());
        }
        return set;
    }
    public static String replaceUnderLineAndUpperCase(String str){
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        int count = sb.indexOf("_");
        while(count!=0){
            int num = sb.indexOf("_",count);
            count = num + 1;
            if(num != -1){
                char ss = sb.charAt(count);
                char ia = (char) (ss - 32);
                sb.replace(count , count + 1,ia + "");
            }
        }
        String result = sb.toString().replaceAll("_","");
        return StringUtils.capitalize(result);
    }
}
